package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.PhoneUtil;
import com.shanjupay.common.util.StringUtil;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.convert.MerchantCovert;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.MerchantMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商户服务
 * </p>
 *
 * @author <a href=mailto:zhaoboying@itcast.cn> byZhao </a>
 * @version 1.0.0
 * @motto Talk is cheap, show me the code!
 * @since 2022-04-29 02:16:28
 */
@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public MerchantDTO queryMerchantById(Long merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        MerchantDTO merchantDTO = new MerchantDTO();
        BeanUtils.copyProperties(merchant, merchantDTO);
        return merchantDTO;
    }

    //实现商户注册方法
    @Transactional
    @Override
    public MerchantDTO createMerchant(MerchantDTO merchantDTO) {
        //参数校验
        if (merchantDTO == null) {
            throw new BusinessException(CommonErrorCode.E_200201);
        }
        if (StringUtils.isBlank(merchantDTO.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_200230);
        }
        // 校验手机号格式是否正确
        if (!PhoneUtil.isMatches(merchantDTO.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }
        // 校验联系人是否为空
        if (StringUtils.isBlank(merchantDTO.getUsername())) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        // 校验密码是否为空
        if (StringUtils.isBlank(merchantDTO.getPassword())) {
            throw new BusinessException(CommonErrorCode.E_100111);
        }
        // 校验商户手机号的唯一性,根据商户的手机号查询商户表，如果存在记录则说明已有相同的手机号重复
        LambdaQueryWrapper<Merchant> wrapper = Wrappers.<Merchant>lambdaQuery().eq(Merchant::getMobile, merchantDTO.getMobile());
        Integer count = merchantMapper.selectCount(wrapper);
        if (count > 0) {
            throw new BusinessException(CommonErrorCode.E_100113);
        }
        // 优化前
        //   Merchant merchant = new Merchant();
        //   BeanUtils.copyProperties(merchantDTO, merchant);
        // 优化后
        Merchant merchant = MerchantCovert.INSTANCE.dto2entity(merchantDTO);
        merchant.setAuditStatus("0");
        merchantMapper.insert(merchant);
        merchantDTO.setId(merchant.getId());
        return merchantDTO;
    }

    /**
     * 资质申请接口
     * @param merchantDTO （商户id必传）
     * @throws BusinessException
     */
    @Transactional
    @Override
    public void applyMerchant(MerchantDTO merchantDTO) throws BusinessException {
        if (merchantDTO == null || merchantDTO.getId()==null){
            throw new BusinessException(CommonErrorCode.E_110006);
        }
        //从数据库查询 是否 存在
        Merchant merchantFromDB = merchantMapper.selectById(merchantDTO.getId());
        if (merchantFromDB == null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        Merchant merchant = MerchantCovert.INSTANCE.dto2entity(merchantDTO);
        merchant.setAuditStatus("1"); // 已申请待审核
        merchantMapper.updateById(merchant);
    }

}
